﻿using System.Diagnostics;
using Utils.Extensions;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System;

namespace StockAccessors.Interdin.Model
{
    internal class WebItem
    {
        #region Attributes

        private readonly List<string> _lines;

        #endregion

        #region Properties

        public DateTime Acquisition { get; set; }

        protected List<string> Lines
        {
            get
            {
                return _lines;
            }
        }

        public string Name
        {
            get
            {
                int startIndex;
                int endIndex;

                if (this.Lines[0].Contains("<b>"))
                {
                    startIndex = this.Lines[0].NthIndexOf(">", 3) + 1;
                    endIndex = this.Lines[0].IndexOf("<", startIndex, System.StringComparison.Ordinal);
                }
                else
                {
                    startIndex = this.Lines[0].NthIndexOf(">", 2) + 1;
                    endIndex = this.Lines[0].IndexOf("<", startIndex, System.StringComparison.Ordinal);
                }

                return this.Lines[0].Substring(startIndex, endIndex - startIndex).Replace("'",""); // Remove invalid characters for the database
            }
        }

        public string Symbol
        {
            get
            {
                int startIndex = this.Lines[0].NthIndexOf("'", 11) + 1;
                int endIndex = this.Lines[0].IndexOf("'", startIndex, System.StringComparison.Ordinal);
                return this.Lines[0].Substring(startIndex, endIndex - startIndex);
            }
        }

    	public bool IsBidVolumeAndValueTogether
    	{
    		get
    		{
				return this.Lines[4].Contains("colspan");
    		}
    	}

		public bool IsAskVolumeAndValueTogether
		{
			get
			{
				if (this.IsBidVolumeAndValueTogether)
				{
					return this.Lines[5].Contains("colspan");	
				}
				else
				{
					return this.Lines[6].Contains("colspan");					
				}
			}
		}

    	public bool IsBidAndAskVolumesAndValuesTogether
    	{
    		get
    		{
				return this.IsBidVolumeAndValueTogether && this.IsAskVolumeAndValueTogether;
    		}
    	}

		public string BidVolume
		{
			get
			{
				return this.ExtractTableCellValue(4);
			}
		}

		public string BidValue
		{
			get
			{
				return this.ExtractTableCellValue(this.IsBidVolumeAndValueTogether ? 4 : 5);
			}
		}

		public string AskValue
		{
			get
			{
				return this.ExtractTableCellValue(this.IsBidVolumeAndValueTogether ? 5 : 6);
			}
		}

		public string AskVolume
		{
			get
			{
				if (this.IsBidAndAskVolumesAndValuesTogether)
				{
					return this.ExtractTableCellValue(5);
				}
				else if (this.IsBidVolumeAndValueTogether || this.IsAskVolumeAndValueTogether)
				{
					return this.ExtractTableCellValue(6);
				}
				else
				{
					return this.ExtractTableCellValue(7);
				}
			}
		}

		public string Maximum
		{
			get
			{
				if (this.IsBidAndAskVolumesAndValuesTogether)
				{
					return this.ExtractTableCellValue(6);
				}
				else if (this.IsBidVolumeAndValueTogether || this.IsAskVolumeAndValueTogether)
				{
					return this.ExtractTableCellValue(7);
				}
				else
				{
					return this.ExtractTableCellValue(8);
				}
			}
		}

		public string Minimum
		{
			get
			{
				if (this.IsBidAndAskVolumesAndValuesTogether)
				{
					return this.ExtractTableCellValue(7);
				}
				else if (this.IsBidVolumeAndValueTogether || this.IsAskVolumeAndValueTogether)
				{
					return this.ExtractTableCellValue(8);
				}
				else
				{
					return this.ExtractTableCellValue(9);
				}
			}
		}

		public string Volume
		{
			get
			{
				if (this.IsBidAndAskVolumesAndValuesTogether)
				{
					return this.ExtractTableCellValue(8);
				}
				else if (this.IsBidVolumeAndValueTogether || this.IsAskVolumeAndValueTogether)
				{
					return this.ExtractTableCellValue(9);
				}
				else
				{
					return this.ExtractTableCellValue(10);
				}
			}
		}

		public string PreviousValue
		{
			get
			{
				if (this.IsBidAndAskVolumesAndValuesTogether)
				{
					return this.ExtractTableCellValue(9);
				}
				else if (this.IsBidVolumeAndValueTogether || this.IsAskVolumeAndValueTogether)
				{
					return this.ExtractTableCellValue(10);
				}
				else
				{
					return this.ExtractTableCellValue(11);
				}
			}
		}

		// <a href="#" onclick="nuevaVentana('contractNumber', '6', '2011', '12', '0', 'FDAX201112');" class='linktexto'><b>Dax</b></a>
    	public DetailsLink DetailsLink
    	{
    		get
    		{
				string[] temp = this.Lines[0].Split('\'');

				return new DetailsLink(int.Parse(temp[1]), int.Parse(temp[3]), int.Parse(temp[5]), int.Parse(temp[7]), temp[9], temp[11]);
    		}
    	}

        #endregion

        #region Public Methods

        public WebItem(XElement data)
        {
            _lines = (from td in data.Descendants("td")
					  select td.ToString()).ToList();
        }

        protected string ExtractTableCellValue(int index)
        {
            int startIndex = this.Lines[index].IndexOf(">", System.StringComparison.Ordinal) + 1;
            int endIndex = this.Lines[index].IndexOf("<", startIndex, System.StringComparison.Ordinal);
            return this.Lines[index].Substring(startIndex, endIndex - startIndex);
        }

        #endregion
    }
}
